{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "new-features",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "7DNLjHkwXuqT",
        "colab_type": "text"
      },
      "source": [
        "# Mounting Google Drive"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "i1C0lbAcBkcE",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from google.colab import drive\n",
        "drive.mount('/content/drive')\n",
        "\n",
        "!ls drive/My\\ Drive\n",
        "\n",
        "# # Uncomment the following for an example of saving a Keras model to your Google Drive using callbacks\n",
        "# # WARNING: THIS WILL EDIT YOUR GOOGLE DRIVE.  USE AT YOUR OWN CAUTION\n",
        "\n",
        "# # Imports\n",
        "# import numpy as np\n",
        "# from keras.layers import Dense\n",
        "# from keras.models import Sequential\n",
        "# from keras.callbacks import ModelCheckpoint\n",
        "\n",
        "# # Fake data\n",
        "# x = np.random.random((32, 4))\n",
        "# y = np.random.random(32)\n",
        "\n",
        "# # Model\n",
        "# model = Sequential([\n",
        "#         Dense(1, input_shape=(4,))\n",
        "# ])\n",
        "\n",
        "# model.compile(optimizer='sgd', loss='mse')\n",
        "\n",
        "# # This is what saves the model to your drive\n",
        "# model_checkpointer = ModelCheckpoint('/content/drive/My Drive/model.h5')\n",
        "\n",
        "# # Training\n",
        "# model.fit(x, y, epochs=2, callbacks=[model_checkpointer])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "RHeA9pf4pNv6",
        "colab_type": "text"
      },
      "source": [
        "# Uploading and Downloading Files\n",
        "Upload files with:\n",
        "\n",
        "```python\n",
        "from google.colab import files\n",
        "files.upload()\n",
        "```\n",
        "\n",
        "And download files with:\n",
        "```python\n",
        "from google.colab import files\n",
        "files.download('path/to/your/file')\n",
        "```\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "UERGcmCnX1Iv",
        "colab_type": "text"
      },
      "source": [
        "# Form-Fields"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "03yNBLIRa_C9",
        "colab_type": "code",
        "cellView": "both",
        "colab": {}
      },
      "source": [
        "#@title Example form fields\n",
        "#@markdown Forms support many types of fields.\n",
        "\n",
        "no_type_checking =   ''#@param\n",
        "string_type = 'example'  #@param {type: \"string\"}\n",
        "slider_value = 160  #@param {type: \"slider\", min: 100, max: 200}\n",
        "number = 102  #@param {type: \"number\"}\n",
        "date = '2010-11-05'  #@param {type: \"date\"}\n",
        "pick_me = \"monday\"  #@param ['monday', 'tuesday', 'wednesday', 'thursday']\n",
        "select_or_input = \"apples\" #@param [\"apples\", \"bananas\", \"oranges\"] {allow-input: true}\n",
        "#@markdown ---"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8a2jXZbYJprp",
        "colab_type": "code",
        "cellView": "both",
        "colab": {}
      },
      "source": [
        "import numpy as np\n",
        "from keras.layers import Dense, Input\n",
        "from keras.models import Model\n",
        "from keras.callbacks import ModelCheckpoint\n",
        "\n",
        "# Fake data\n",
        "x_data = np.random.random((32, 4))\n",
        "y_data = np.random.random((32, 1))\n",
        "\n",
        "# Hyperparameters\n",
        "EPOCHS = 10 #@param {type: \"number\"}\n",
        "BATCH_SIZE = 16 #@param {type: \"slider\", min: 2, max: 32, step: 2}\n",
        "NUM_DENSE_LAYERS = 4 #@param {type: \"slider\", min: 1, max: 6}\n",
        "UNITS_PER_DENSE_LAYER = 16 #@param {type: \"number\"}\n",
        "ACTIVATION = 'elu' #@param ['relu', 'tanh', 'elu', 'sigmoid', 'linear']\n",
        "OPTIMIZER = 'sgd' #@param ['sgd', 'RMSprop', 'adam', 'adagrad', 'adadelta', 'adamax', 'nadam']\n",
        "LOSS = 'mse' #@param ['mse', 'mae', 'squared_hinge', 'hinge', 'cosine_proximity']\n",
        "\n",
        "# Model\n",
        "input_layer = Input(shape=(4,))\n",
        "x = Dense(UNITS_PER_DENSE_LAYER, activation=ACTIVATION)(input_layer)\n",
        "\n",
        "for _ in range(NUM_DENSE_LAYERS-1):\n",
        "    x = Dense(UNITS_PER_DENSE_LAYER, activation=ACTIVATION)(x)\n",
        "x = Dense(1, activation='sigmoid')(x)\n",
        "\n",
        "model = Model(input_layer, x)\n",
        "model.compile(optimizer=OPTIMIZER, loss=LOSS)\n",
        "\n",
        "model.summary()\n",
        "\n",
        "# Training\n",
        "print(x_data.shape, model.input_shape, y_data.shape, model.output_shape)\n",
        "model.fit(x_data, y_data, epochs=EPOCHS, batch_size=BATCH_SIZE)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "mc1DH0fyX8s7",
        "colab_type": "text"
      },
      "source": [
        "# Running JavaScipt and HTML"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Xc4ihZwJa_rO",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import IPython\n",
        "\n",
        "js_code = \\\n",
        "'''\n",
        "let message = \"Hello world!\";\n",
        "document.querySelector(\"#output-area\").appendChild(document.createTextNode(message));\n",
        "'''\n",
        "\n",
        "display(IPython.display.Javascript(js_code))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "N6pPIFKlSinD",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "%%javascript\n",
        "\n",
        "let message = \"Javascript in Google Colab? Awesome!\"\n",
        "console.log(message) // check your console with F12 or ctrl/cmnd+shift+i and swapping to the `Console` tab\n",
        "document.querySelector(\"#output-area\").appendChild(document.createTextNode(message));"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_5IRxTfZP5yd",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "html_code = \\\n",
        "'''\n",
        "<p>Now this is <i>really</i> awesome!</p>\n",
        "<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/0/0a/Python.svg/768px-Python.svg.png'></img>\n",
        "'''\n",
        "\n",
        "display(IPython.display.HTML(html_code))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "425iB8NEYDxu",
        "colab_type": "text"
      },
      "source": [
        "# Using the Webcam"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "HgHQWD7iobiC",
        "colab_type": "text"
      },
      "source": [
        "## Basic Usage\n",
        "The example given by the Colab Team"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WlbhwJYgXTKG",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from IPython.display import display, Javascript\n",
        "from google.colab.output import eval_js\n",
        "from base64 import b64decode\n",
        "\n",
        "def take_photo(filename='photo.jpg', quality=0.8):\n",
        "  js = Javascript('''\n",
        "    async function takePhoto(quality) {\n",
        "      const div = document.createElement('div');\n",
        "      const capture = document.createElement('button');\n",
        "      capture.textContent = 'Capture';\n",
        "      div.appendChild(capture);\n",
        "\n",
        "      const video = document.createElement('video');\n",
        "      video.style.display = 'block';\n",
        "      const stream = await navigator.mediaDevices.getUserMedia({video: true});\n",
        "\n",
        "      document.body.appendChild(div);\n",
        "      div.appendChild(video);\n",
        "      video.srcObject = stream;\n",
        "      await video.play();\n",
        "\n",
        "      // Resize the output to fit the video element.\n",
        "      google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);\n",
        "\n",
        "      // Wait for Capture to be clicked.\n",
        "      await new Promise((resolve) => capture.onclick = resolve);\n",
        "\n",
        "      const canvas = document.createElement('canvas');\n",
        "      canvas.width = video.videoWidth;\n",
        "      canvas.height = video.videoHeight;\n",
        "      canvas.getContext('2d').drawImage(video, 0, 0);\n",
        "      stream.getVideoTracks()[0].stop();\n",
        "      div.remove();\n",
        "      return canvas.toDataURL('image/jpeg', quality);\n",
        "    }\n",
        "    ''')\n",
        "  display(js)\n",
        "  data = eval_js('takePhoto({})'.format(quality))\n",
        "  binary = b64decode(data.split(',')[1])\n",
        "  with open(filename, 'wb') as f:\n",
        "    f.write(binary)\n",
        "  return filename"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Yr42cC1PXTJg",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from IPython.display import Image\n",
        "try:\n",
        "  filename = take_photo()\n",
        "  print('Saved to {}'.format(filename))\n",
        "  \n",
        "  # Show the image which was just taken.\n",
        "  display(Image(filename))\n",
        "except Exception as err:\n",
        "  # Errors will be thrown if the user does not have a webcam or if they do not\n",
        "  # grant the page permission to access it.\n",
        "  print(str(err))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "-kEnyhnwokh1",
        "colab_type": "text"
      },
      "source": [
        "## Save Image to NumPy (NP) Array"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Nqs_qribM3PG",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import tempfile\n",
        "import cv2\n",
        "\n",
        "from IPython.display import display, Javascript\n",
        "from google.colab.output import eval_js\n",
        "from base64 import b64decode\n",
        "\n",
        "def take_photo(quality=0.8):\n",
        "  js = Javascript('''\n",
        "    async function takePhoto(quality) {\n",
        "      const div = document.createElement('div');\n",
        "      const capture = document.createElement('button');\n",
        "      capture.textContent = 'Capture';\n",
        "      div.appendChild(capture);\n",
        "\n",
        "      const video = document.createElement('video');\n",
        "      video.style.display = 'block';\n",
        "      const stream = await navigator.mediaDevices.getUserMedia({video: true});\n",
        "\n",
        "      document.body.appendChild(div);\n",
        "      div.appendChild(video);\n",
        "      video.srcObject = stream;\n",
        "      await video.play();\n",
        "\n",
        "      // Resize the output to fit the video element.\n",
        "      google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);\n",
        "\n",
        "      // Wait for Capture to be clicked.\n",
        "      await new Promise((resolve) => capture.onclick = resolve);\n",
        "\n",
        "      const canvas = document.createElement('canvas');\n",
        "      canvas.width = video.videoWidth;\n",
        "      canvas.height = video.videoHeight;\n",
        "      canvas.getContext('2d').drawImage(video, 0, 0);\n",
        "      stream.getVideoTracks()[0].stop();\n",
        "      div.remove();\n",
        "      return canvas.toDataURL('image/jpeg', quality);\n",
        "    }\n",
        "    ''')\n",
        "  display(js)\n",
        "  data = eval_js('takePhoto({})'.format(quality))\n",
        "  binary = b64decode(data.split(',')[1])\n",
        "\n",
        "  # Create temporary file for holding image data\n",
        "  img_file = tempfile.NamedTemporaryFile()\n",
        "  img_file.write(binary)\n",
        "\n",
        "  # Read temp. file and convert it to a Numpy array\n",
        "  img = cv2.imread(img_file.name)\n",
        "  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
        "\n",
        "  # Close file\n",
        "  img_file.close()\n",
        "\n",
        "  return img"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "h3R6BfBMjR3I",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import matplotlib.pyplot as plt\n",
        "\n",
        "img = take_photo()\n",
        "\n",
        "plt.imshow(img)\n",
        "plt.show()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "W9cCprCIor5u",
        "colab_type": "text"
      },
      "source": [
        "## Instantly take a photo and save it to a NumPy Array"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "y9pgei0XlusY",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import tempfile\n",
        "import cv2\n",
        "\n",
        "from IPython.display import display, Javascript\n",
        "from google.colab.output import eval_js\n",
        "from base64 import b64decode\n",
        "\n",
        "def take_photo(quality=0.8):\n",
        "  js = Javascript('''\n",
        "    async function takePhoto(quality) {\n",
        "      const div = document.createElement('div');\n",
        "\n",
        "      const video = document.createElement('video');\n",
        "      video.style.display = 'block';\n",
        "      const stream = await navigator.mediaDevices.getUserMedia({video: true});\n",
        "\n",
        "      document.body.appendChild(div);\n",
        "      div.appendChild(video);\n",
        "      video.srcObject = stream;\n",
        "      await video.play();\n",
        "\n",
        "      // Resize the output to fit the video element.\n",
        "      google.colab.output.setIframeHeight(document.documentElement.scrollHeight, true);\n",
        "\n",
        "      // Waiting any shorter than this seems to make the image really red\n",
        "      // You may want to remove this depending on your webcam\n",
        "      await new Promise(r => setTimeout(r, 175))\n",
        "\n",
        "      const canvas = document.createElement('canvas');\n",
        "      canvas.width = video.videoWidth;\n",
        "      canvas.height = video.videoHeight;\n",
        "      canvas.getContext('2d').drawImage(video, 0, 0);\n",
        "      stream.getVideoTracks()[0].stop();\n",
        "      div.remove();\n",
        "      return canvas.toDataURL('image/jpeg', quality);\n",
        "    }\n",
        "    ''')\n",
        "  display(js)\n",
        "  data = eval_js('takePhoto({})'.format(quality))\n",
        "  binary = b64decode(data.split(',')[1])\n",
        "\n",
        "  # Create temporary file for holding image data\n",
        "  img_file = tempfile.NamedTemporaryFile()\n",
        "  img_file.write(binary)\n",
        "\n",
        "  # Read temp. file and convert it to a Numpy array\n",
        "  img = cv2.imread(img_file.name)\n",
        "  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
        "\n",
        "  # Close file\n",
        "  img_file.close()\n",
        "\n",
        "  return img"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ikElorr7moX3",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import matplotlib.pyplot as plt\n",
        "\n",
        "img = take_photo()\n",
        "\n",
        "plt.imshow(img)\n",
        "plt.show()"
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}